package com.hs.biye.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.mapper.EmployeeMapper;
import com.hs.biye.mapper.PatientMapper;
import com.hs.biye.mapper.SeedoctorMapper;
import com.hs.biye.model.DTO.SeedoctorRequest;
import com.hs.biye.model.VO.SeedoctorVo;
import com.hs.biye.model.entity.*;
import com.hs.biye.model.entity.Seedoctor;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.SeedoctorService;
import com.hs.biye.mapper.SeedoctorMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author haoshuai
* @description 针对表【seedoctor】的数据库操作Service实现
* @createDate 2024-02-08 20:28:58
*/
@Service
public class SeedoctorServiceImpl extends ServiceImpl<SeedoctorMapper, Seedoctor>
    implements SeedoctorService{

    @Resource
    private SeedoctorMapper seedoctorMapper;

    @Resource
    private EmployeeMapper employeeMapper;


    @Resource
    private PatientMapper patientMapper;

    /**
     * 查询
     * @param seedoctorRequest
     * @return
     */
    @Override
    public ResultPage<SeedoctorVo> getPageSeeDoctor(SeedoctorRequest seedoctorRequest) {
        if (seedoctorRequest==null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Integer page = seedoctorRequest.getPages();
        Integer size = seedoctorRequest.getSize();
        IPage<Seedoctor> pages = new Page<>(page,size);
        LambdaQueryWrapper<Seedoctor> queryWrapper = getQueryWrapper(seedoctorRequest);
        seedoctorMapper.selectPage(pages,queryWrapper);

        List<Seedoctor> records = pages.getRecords();
        long total = pages.getTotal();
        long current = pages.getCurrent();
        long page2 = pages.getPages();

        List<SeedoctorVo> reslutList = new ArrayList<>();
        records.forEach(item -> {
            SeedoctorVo seedoctorVO = new SeedoctorVo();
            BeanUtils.copyProperties(item,seedoctorVO);
            //查询医生的姓名
            Employee employee = employeeMapper.selectById(item.getEmployeeId());
            //查询患者的姓名和身份证号
            Patient patient = patientMapper.selectById(item.getPatientId());
            seedoctorVO.setEmployeeName(employee.getEmployeeName());
            seedoctorVO.setPatientName(patient.getPatientName());
            seedoctorVO.setIdentity(patient.getIdentity());
            //就诊信息格式转化
            Description description = JSONUtil.toBean(item.getDescription(), Description.class);
            seedoctorVO.setDescription(description);
            reslutList.add(seedoctorVO);
        });
        return ResultPage.success(reslutList, total,current,page2);
    }


    //添加
    @Override
    @Transactional
    public boolean addSeeDoctor(SeedoctorRequest seedoctorRequest) {
        if (seedoctorRequest==null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }

        Seedoctor medicine = new Seedoctor();
        BeanUtils.copyProperties(seedoctorRequest,medicine);
        Description description = seedoctorRequest.getDescription();
        if (description==null){
            throw new BaseException(MessageConstant.DESCRIPTION_NOT_NULL);
        }
        //就诊信息格式转化
        String jsonStr = JSONUtil.toJsonStr(description);
        medicine.setDescription(jsonStr);
        return save(medicine);
    }


    // 更新
    @Override
    @Transactional
    public boolean updateSeeDoctor(SeedoctorRequest seedoctorRequest) {
        if (seedoctorRequest==null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }

        Seedoctor medicine = new Seedoctor();
        BeanUtils.copyProperties(seedoctorRequest,medicine);
        Description description = seedoctorRequest.getDescription();
        if (description==null){
            throw new BaseException(MessageConstant.DESCRIPTION_NOT_NULL);
        }
        //就诊信息格式转化
        String jsonStr = JSONUtil.toJsonStr(description);
        medicine.setDescription(jsonStr);
        return updateById(medicine);
    }




    //删除
    @Override
    public boolean removeSeeDoctor(Integer id) {
        if (id==null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        return removeById(id);
    }


    private LambdaQueryWrapper<Seedoctor> getQueryWrapper(SeedoctorRequest seedoctorRequest){

        LambdaQueryWrapper<Seedoctor> seedoctorLambdaQueryWrapper = new LambdaQueryWrapper<>();

        Integer seeDoctorId = seedoctorRequest.getSeeDoctorId();
        Integer patientId = seedoctorRequest.getPatientId();
        Integer employeeId = seedoctorRequest.getEmployeeId();
        LocalDateTime createTime = seedoctorRequest.getCreateTime();
        LocalDateTime updateTime = seedoctorRequest.getUpdateTime();
        String isDelete = seedoctorRequest.getIsDelete();
        Integer pages = seedoctorRequest.getPages();
        Integer size = seedoctorRequest.getSize();
        seedoctorLambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(employeeId),Seedoctor::getEmployeeId,employeeId);
        return seedoctorLambdaQueryWrapper;
    }

}




